package com.kucai.yifaqapp.widget;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.changeNikeNameBean;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by admin on 2018/12/26.
 */

public class changeNikeDiolog extends Activity {
    private EditText changenike;
    private Button cancle;
    private Button conform;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_nike_layout);
        changenike = findViewById(R.id.changenike);
        cancle = findViewById(R.id.btn_neg);
        conform = findViewById(R.id.btn_pos);
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        conform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (changenike.getText().toString().isEmpty()) {
                    Toast.makeText(changeNikeDiolog.this, "输入昵称不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    //开始网络请求
                    HttpJsonUtils.getHttpApiInstance()
                            .change_niocheng( HttpAdress.getuseAdress() + "api/mobile/changeName",Info.user.token, changenike.getText().toString().trim())
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new CustomObserver<changeNikeNameBean>() {
                                @Override
                                protected void onCustomNext(changeNikeNameBean resBanner) {
                                    if (resBanner.isStatus()) {
                                        Toast.makeText(changeNikeDiolog.this, "修改昵称成功", Toast.LENGTH_LONG).show();
                                        finish();
                                    } else {
                                        Toast.makeText(changeNikeDiolog.this, "" + resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                                    }
                                }

                                @Override
                                protected void onSubscribeCustom(Disposable disposable) {
                                }

                                @Override
                                protected void onErrorCustom(Throwable e) {
                                }
                            });
                }
            }
        });
    }
}
